<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      :border="false"
      fit
      style="width: 100%;"
    >
      <el-table-column label="ID" width="200">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="200">
        <template slot-scope="{row}">
          <span>{{ row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="说明">
        <template slot-scope="{row}">
          <span>{{ row.text }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作ID">
        <template slot-scope="{row}">
          <span>{{ row.model_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发起时间" width="160">
        <template slot-scope="{row:{create_time}}">
          <i class="el-icon-time" />
          <span style="margin-left: 5px">{{ create_time | timeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="200"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleView(row)">
            查看
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-edit" @click="handleCheck(row)">
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { parseTime } from '@/utils'
import waves from '@/directive/waves'
import api from '@/api'

export default {
  name: 'SupplierList',
  directives: { waves },
  filters: {
    timeFilter(time) {
      return time ? parseTime(time, '{m}-{d} {h}:{i}') : ''
    }
  },
  data() {
    return {
      listLoading: false,
      list: []
    }
  },
  created() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      api.supplier.checkList().then(response => {
        const { data } = response
        this.list = data.list
      }).finally(() => {
        this.listLoading = false
      })
    },
    handleCheck(row) {
      this.$confirm('您确定要通过审核吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.supplier.checkOk({ id: row.id }).then(response => {
          this.$message({
            message: response.message || '操作成功',
            type: 'success'
          })
          this.list = this.list.filter(x => x.id !== row.id)
        })
      }).catch(() => {})
    },
    handleView(row) {
      if (row.type === 'Brand') {
        this.$router.push(`/product/brand/edit/${row.model_id}`)
      } else if (row.type === 'Category') {
        this.$router.push(`/product/category/edit/${row.model_id}`)
      } else if (row.type === 'Product') {
        this.$router.push(`/product/edit/${row.model_id}`)
      }
    }
  }
}
</script>
